﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.Models.Reposistory.User;
using ListenMusic.Models.Reposistory.Public;
using ListenMusic.User_ServiceReference;
using ListenMusic.Models.ViewModel;
using ListenMusic.Singer_ServiceReference;

namespace ListenMusic.Controllers.Public
{
    public class SearchController : ApplicationController
    {
        //
        // GET: /Search/
        
        public SearchController()
        {
           
        }       
        public ActionResult Search_bySingerName(string option ,string singerName, int pageindex)
        {
            ViewBag.singerName = singerName;
            ViewBag.option = option;
            int pagesize = 10;
            int start = (pageindex - 1) * pagesize;
            int end = pageindex * pagesize;
            if (option == "song")
            {
                double a = (_song.countAllSong_bySingerName(singerName, 1)) / pagesize;
                ViewBag.totalSong = Math.Ceiling(a) + 1;
                List<Song_ServiceReference.SongSinger> lst_result = _song.getAllSong_bySingerName(start,end,singerName, 1);                
                ViewBag.searchResult = lst_result;               
                return View("SearchBySinger");                
            }
            else if (option == "video")
            {
                double a = (_video.countVideo_bySingerName(singerName, 2)) / pagesize;
                ViewBag.totalVideo = Math.Ceiling(a) + 1;
                List<Video_ServiceReference.SongSinger> lst_result = _video.getAllVideo_bySingerName(start,end,singerName, 2);
                ViewBag.searchResult = lst_result;
                return View("SearchBySinger");
            }
            else if(option =="album")
            {
                double a = (_album.countAlbumSinger_bySingerName(singerName)) / pagesize;
                ViewBag.totalAlbum = Math.Ceiling(a) + 1;
                List<Album_ServiceReference.AlbumSinger> lst_result = _album.getAllAlbumSinger_bySingerName(start, end, singerName);
                ViewBag.searchResult = lst_result;
                return View("SearchBySinger");
            }
            else
            {
                return RedirectToAction("SearchNotFound");
            }
        }
        public ActionResult Search_bySongName(int pageindex,string option,string keyword)
        {
            ViewBag.keyword = keyword;
            ViewBag.option = option;
            int pagesize = 10;
            int start = (pageindex - 1) * pagesize;
            int end = pagesize * pageindex;
            if (option == "song")
            {
                double a = (_song.countAllSong_bySongName(keyword, 1)) / pagesize;
                ViewBag.totalSong = Math.Ceiling(a) + 1;
                ViewBag.searchResult = _song.getAllSong_bySongName(start, end, keyword, 1);
                return View("SearchBySong");
            }
            else if (option == "album")
            {
                double a = (_video.countAllVideo_byVideoName(keyword, 2)) / pagesize;
                ViewBag.totalAlbum = Math.Ceiling(a) + 1;
                ViewBag.searchResult = _album.search_AlbumName(start, end, keyword);
                return View("SearchBySong");
            }
            else if (option =="video"){
                double a = (_album.countAlbum_byAlbumName(keyword)) / pagesize;
                ViewBag.totalVideo = Math.Ceiling(a) + 1;
                ViewBag.searchResult = _video.getAllVideo_byVideoName(start, end, keyword, 2);
                return View("SearchBySong");
            }
            else
            {
                return RedirectToAction("SearchNotFound");
            }
        }       
        public ActionResult SearchNotFound()
        {
            return View();
        }
    }
}
